package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderNumberBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.PriceBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WXPayBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IAppointmentView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/31.
 */

public class AppointmentPresenter extends BasePresenter<IAppointmentView> {

    public AppointmentPresenter(IAppointmentView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void caculatePrice(String token, String projectId, String couponId) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .price(token, projectId, couponId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<PriceBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<PriceBean> response) {
                        iView.setPrice(response.getData());
                    }
                }));
    }

    public void getOrderNumber(String token, String technicianId, String storeId, String projectId,
                               String time, String couponId, String price, String payment) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("t_id", technicianId);
        map.put("r_id", storeId);
        map.put("p_id", projectId);
        map.put("tment_time", time);
        map.put("coupon", couponId);
        map.put("price", price);
        map.put("payment", payment);
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .make_an(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<OrderNumberBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<OrderNumberBean> response) {
                        OrderNumberBean data = response.getData();
                        String order_num = data.getOrder_num();
                        String money = data.getPrice();
                        if (payment.equals("1")) {
                            wxPay(order_num, money);
                        } else if (payment.equals("2")) {
                            alipay(order_num, money);
                        }
                    }
                }));
    }

    private void alipay(String orderNum, String money) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .alipaynotify_url(orderNum, money)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.aliPay(response.getData());
                    }
                }));
    }

    private void wxPay(String orderNum, String money) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .WxPay(money, orderNum, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<WXPayBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<WXPayBean> response) {
                        iView.wxPay(response.getData(),orderNum);
                    }
                }));
    }

}
